package com.buer.storage.controller;

import com.buer.storage.commom.JsonResult;
import com.buer.storage.dto.SupplierDto;
import com.buer.storage.entity.Prod;
import com.buer.storage.entity.Supplier;
import com.buer.storage.service.SupplierService;
import com.buer.storage.vo.R;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/supplier")
public class SupplierController {
    @Resource
    private SupplierService supplierService;

    //查询所有供应商
    @GetMapping("/findSupplier.do")
    public List<Supplier> findSupplier() {
        return supplierService.findSupplier();
    }

    //增加供应商
    @PostMapping("/add.do")
    public R add(@RequestBody SupplierDto supplierDto) {
        if (supplierService.insert(supplierDto) > 0) {
            return R.setR(1, "添加供应商成功", null);
        }
        return R.setR(0, "添加供应商失败", null);
    }
    //删除供应商
    @GetMapping("/delete.do")
    public R delete(int supplierId) {
        if (supplierService.delete(supplierId) > 0) {
            return R.setR(1, "删除供应商成功", null);
        }
        return R.setR(0, "删除供应商失败", null);
    }

    //修改供应商
    @PostMapping("/update.do")
    public R update(@RequestBody Supplier supplier){
        if (supplierService.update(supplier)>0){
            return R.setR(1,"修改供应商成功",null);
        }
        return R.setR(0,"修改供应商失败",null);
    }

    //查询所有供应商
    @PostMapping("/findAllSupplier.do")
    public JsonResult findAllSupplier(@RequestBody Map<String,Object> params){
        PageInfo<Supplier> entityList = supplierService.findAllSupplier(params);
        Map result=new HashMap<>();
        result.put("total",entityList.getTotal());
        result.put("list",entityList.getList());
        return new JsonResult(1,result);
    }

    @PostMapping("/searchSupplier.do")
    public JsonResult searchSupplier(@RequestBody Map<String,Object> params){
        PageInfo<Supplier> entityList = supplierService.searchSupplier(params);
        Map result=new HashMap<>();
        result.put("total",entityList.getTotal());
        result.put("list",entityList.getList());
        return new JsonResult(1,result);
    }
}
